#ifndef STATEFULUNIT_H
#define STATEFULUNIT_H

#include <string>
#include "BaseUnit.h"
#include "StatefulSprite.h"
#include "Vector2df.h"

class StatefulUnit : public BaseUnit
{
    public:
        StatefulUnit();
        StatefulUnit(const Vector2df p, const Vector2df v);
        StatefulUnit(string spriteFilename, const Vector2df p, const Vector2df v, CRstring parameter);
        StatefulUnit(string spriteFilename);
        virtual ~StatefulUnit();

        // getters
        virtual int getWidth() const;
        virtual int getHeight() const;
        virtual Vector2df getSize() const;
        virtual Vector2df getPositionOffset(CRbool inTiles = false) const;

        // collision
        virtual void hit(const SimpleDirection dir, BaseUnit* const player, CRstring parameter="");
        virtual void hitMap(MapData* const data, const CollisionObject* const obj);
        virtual void checkCollision(vector< BaseUnit* >* const enemies, StatefulUnit* hero);
        bool hasCollisionFlag(CRint flag){if (m_statefulSprite) return m_statefulSprite->m_collisionFlags.hasFlag(flag); else return false;}
        CollisionRegion* getCollision() const {return m_collisionRegion; }

        // Framework
        virtual void update();
        void render();

        // sprite stuff
        void loadSprite(string spriteFilename);
        void setSpriteState(string stateName);
        string getSpriteState() const { if (m_statefulSprite) return m_statefulSprite->getSpriteState(); else return "None";};

        // event stuff
        bool hasFinished() const { if (m_sprite) return m_sprite->hasFinished(); else return false; };
        void playSound(string soundName) { if (m_statefulSprite) m_statefulSprite->playSound(soundName);};
        virtual void handleEvent(string eventName);
    protected:
        // you can overwrite this function in a child class to add class specific behaviour
        // the function returns true if the event was processed successfull and false otherwise
        // so you can keep a call to this parent function in the child class and check the return value to process all events
        virtual bool processEvent(vector< string > event);

        void releaseFromBeingCarried(const SimpleDirection& dir);

        bool m_isGeneric;
        StatefulSprite *m_statefulSprite;
        CollisionRegion *m_collisionRegion;
        int lastFrame; // used to limit frameSound and frameEffect calls to one per frame
    private:
        void init();
};

#endif // STATEFULUNIT_H
